package com.supermarket.business.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.supermarket.business.domain.SupermarketAdmin;
import com.supermarket.business.domain.SupermarketCashier;
import com.supermarket.business.domain.SupermarketWarehouseManager;
import com.supermarket.business.mapper.SupermarketAdminMapper;
import com.supermarket.business.mapper.SupermarketWarehouseManagerMapper;
import com.supermarket.business.vo.SupermarketCashierVO;
import com.supermarket.common.core.domain.entity.SysUser;
import com.supermarket.common.utils.DateUtils;
import com.supermarket.common.utils.SecurityUtils;
import com.supermarket.system.mapper.SysUserMapper;
import com.supermarket.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.supermarket.business.mapper.SupermarketCashierMapper;
import com.supermarket.business.service.ISupermarketCashierService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 收银员Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-21
 */
@Service
public class SupermarketCashierServiceImpl extends ServiceImpl<SupermarketCashierMapper, SupermarketCashier> implements ISupermarketCashierService
{
    @Resource
    private SupermarketCashierMapper supermarketCashierMapper;
    @Resource
    private SupermarketAdminMapper supermarketAdminMapper;
    @Resource
    private SupermarketWarehouseManagerMapper supermarketWarehouseManagerMapper;
    @Resource
    private ISysUserService sysUserService;
    @Resource
    private SysUserMapper sysUserMapper;

    /**
     * 查询收银员
     * 
     * @param cashierId 收银员主键
     * @return 收银员
     */
    @Override
    public SupermarketCashier selectSysCashierByCashierId(Long cashierId)
    {
        return supermarketCashierMapper.selectSysCashierByCashierId(cashierId);
    }

    /**
     * 查询收银员列表
     * 
     * @param supermarketCashier 收银员
     * @return 收银员
     */
    @Override
    public List<SupermarketCashier> selectSysCashierList(SupermarketCashier supermarketCashier)
    {
        return supermarketCashierMapper.selectSysCashierList(supermarketCashier);
    }

    /**
     * 新增收银员
     * 
     * @param supermarketCashier 收银员
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSysCashier(SupermarketCashier supermarketCashier)
    {
        //校验账号id是否合理
        if(Objects.nonNull(supermarketCashier.getSysId())){
            SysUser user = sysUserMapper.getUserById(supermarketCashier.getSysId());
            if(Objects.nonNull(user.getBeUsed())&&!Objects.equals(user.getBeUsed(),supermarketCashier.getCashierId())){
                throw new IllegalArgumentException("该账号已被使用，请更换账号！");
            }
            sysUserMapper.updateBeUsed(supermarketCashier.getCashierId(), user.getUserId());
        }
        return supermarketCashierMapper.insertSysCashier(supermarketCashier);
    }

    /**
     * 修改收银员
     * 
     * @param supermarketCashier 收银员
     * @return 结果
     */
    @Override
    @Transactional
    public int updateSysCashier(SupermarketCashier supermarketCashier)
    {
        //将之前的账号id解锁
        SupermarketCashier supermarketCashier1 = supermarketCashierMapper.selectSysCashierByCashierId(supermarketCashier.getCashierId());
        SysUser user1 = sysUserMapper.getUserById(supermarketCashier1.getSysId());
        if(Objects.nonNull(user1)){
            sysUserMapper.updateBeUsed(null, user1.getUserId());
        }

        //校验账号id是否合理
        if(Objects.nonNull(supermarketCashier.getSysId())){
            SysUser user = sysUserMapper.getUserById(supermarketCashier.getSysId());
            if(Objects.nonNull(user.getBeUsed())&&!Objects.equals(user.getBeUsed(),supermarketCashier.getCashierId())){
                throw new IllegalArgumentException("该账号已被使用，请更换账号！");
            }
            sysUserMapper.updateBeUsed(supermarketCashier.getCashierId(), user.getUserId());
        }
        return supermarketCashierMapper.updateSysCashier(supermarketCashier);
    }

    /**
     * 批量删除收银员
     * 
     * @param cashierIds 需要删除的收银员主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteSysCashierByCashierIds(Long[] cashierIds)
    {
        for (Long cashierId : cashierIds) {
            updateSysId(cashierId);
        }
        return supermarketCashierMapper.deleteSysCashierByCashierIds(cashierIds);
    }

    /**
     * 删除收银员信息
     * 
     * @param cashierId 收银员主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteSysCashierByCashierId(Long cashierId)
    {
        updateSysId(cashierId);
        return supermarketCashierMapper.deleteSysCashierByCashierId(cashierId);
    }

    /**
     * 获取收银员VO列表
     * @param supermarketCashier
     * @return
     */
    @Override
    public List<SupermarketCashierVO> selectSysCashierListVO(SupermarketCashier supermarketCashier) {
        List<SupermarketCashier> list = supermarketCashierMapper.selectSysCashierList(supermarketCashier);
        List<SupermarketCashierVO> listVO = new ArrayList<SupermarketCashierVO>();
        for (SupermarketCashier cashier : list) {
            SupermarketCashierVO cashierVO = new SupermarketCashierVO();
            BeanUtils.copyProperties(cashier, cashierVO);
            listVO.add(cashierVO);
        }
        return listVO;
    }

    @Override
    public SupermarketCashierVO selectSysCashierVOByCashierId(Long cashierId) {
        SupermarketCashier cashier = supermarketCashierMapper.selectSysCashierByCashierId(cashierId);
        if (cashier == null) {
            throw new IllegalArgumentException("收银员不存在");
        }
        SupermarketCashierVO cashierVO = new SupermarketCashierVO();
        BeanUtils.copyProperties(cashier, cashierVO);
        return cashierVO;
    }

    @Override
    public String getNowSysId() {
        return SecurityUtils.getLoginUser().getUserId().toString();
    }

    /**
     * 释放账号id
     */
    private void updateSysId(Long cashierId){
        SupermarketCashier supermarketCashier = supermarketCashierMapper.selectSysCashierByCashierId(cashierId);
        if(Objects.nonNull(supermarketCashier.getSysId())){
            sysUserMapper.updateBeUsed(null, supermarketCashier.getSysId());
        }
    }
}
